<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <style>
        .Modular {
            width: 100%;
            margin: 0px 0px 10px 0px;
            padding: 20px 0px;
            background: #fff;
        }

        .Modular>div {
            width: 80% !important;
            margin: auto;
        }

        .m {
            width: 80%;
            height: 540px;
            margin: auto;
            margin-top: 20px;
        }

        .m_cover {
            position: relative;
            display: inline-block;
            width: 73%;
        }

        .m_cover img {
            width: 100%;
        }

        .m_edit {
            width: 25%;
            background: #fff;
            float: right;
            box-shadow: 3px 6px 11px 0 #d4d4d4;
        }

        .m_author {
            width: 73%;
            height: 185px;
            background: #fff;
            box-shadow: 3px 6px 11px 0 #d4d4d4;
            margin-top: 20px;
        }

        .m_dec {
            width: 73%;
            background: #fff;
            box-shadow: 3px 6px 11px 0 #d4d4d4;
            margin-top: 20px;
        }

        .m_edit .layui-tab {
            margin: 5px 0px;
        }

        .m_edit .layui-this:after {
            border-bottom: 0px !important;
        }

        .m_edit .layui-tab-title {
            border-bottom-style: none;
            padding-top: 10px;
        }

        .m_edit .layui-tab-title li {
            background: #e8e8e8;
            width: 47%;
            box-sizing: border-box;
            margin-left: 2%;
            border-radius: 4px;
        }

        .m_edit .layui-this {
            background: #1E9FFF !important;
            color: #fff !important;
        }

        .m_c,
        .m_edit_btn {
            width: 100%;
            padding: 5px;
            box-sizing: border-box;
        }

        .m_c {
            height: 195px;
            position: relative;
            cursor: pointer;
            overflow: hidden;
        }

        .m_c img {
            width: 100%;
            height: 100%;
            opacity: .6;
        }

        .m_edit_btn button {
            width: 30%;
        }

        .m_title {
            line-height: 40px;
            font-size: 14px;
            color: #868686;
            font-weight: bold;
            height: 40px;
            text-indent: 2px;
        }

        .m_edit .layui-textarea {
            min-height: 150px;
        }

        .m_edit_btn {
            padding: 10px 20px;
        }

        .m_name {
            font-size: 20px;
            font-weight: bold;
            width: 50%;
            height: 60px;
            line-height: 60px;
            text-indent: 20px;
        }

        .m_data {
            width: 50%;
            float: right;
            height: 60px;
        }

        .m_data>div {
            float: right;
            margin-right: 30px;
            font-size: 16px;
            font-weight: bold;
            height: 60%;
            line-height: 60px;
        }

        .m_author_photo {
            width: 70px;
            height: 70px;
            float: left;
            margin-left: 20px;
        }

        .m_author_photo img {
            width: 100%;
            border-radius: 100%;
        }

        .m_author_name {
            float: left;
            margin-left: 15px;
            margin-top: 5px;
        }

        .m_author_name p {
            font-size: 16px;
            margin-top: 20px;
            color: #868686;
            font-weight: bold;
        }

        .m_dec .layui-this {
            color: #1E9FFF !important;
            font-weight: bold;
            font-size: 16px;
        }

        .m_dec .layui-this:after {
            border-color: #1E9FFF !important;
        }

        .m_dec_con {
            width: 100%;
            padding: 20px 0px;
            border-bottom: 1px #ccc solid;
        }

        .m_dec_type {
            width: 100%;
            padding: 20px 0px;
        }

        .m_dec_type>div {
            margin-bottom: 10px;
        }

        .m_dec .layui-tab-content {
            margin-left: 10px;
        }

        .local {
            display: inline-block;
        }

        .local label {
            width: 35px !important;
        }

        .local input {
            width: 100px;
        }

        .local .layui-input-block {
            margin-left: 0px;
            display: inline-block;
        }

        .m_engin input,
        .nav_tool input {
            width: 250px;
            display: inline-block;
            margin-right: 10px;
        }

        .m_engin .layui-tab-title li,
        .nav_tool .layui-tab-title li,
        .nav_func .layui-tab-title li {
            width: unset !important;
            padding: 0px 15px !important;
        }

        .m_vector .layui-input-block {
            width: 100px;
            display: inline-block;
            margin-left: 0px;
        }

        .m_vector input {
            width: 100px;
        }

        .m_vector label {
            width: 35px !important;
        }

        .m_vector .layui-form-item {
            display: inline-block;
        }

        .show_func {
            display: none;
        }

        .nav_tool input,
        .nav_func input {
            width: 150px;
        }

        .nav_tool .layui-form-select,
        .nav_func .layui-form-select {
            margin-left: 0px;
            display: inline-block;
            margin-right: 10px;
            width: 150px;
        }

        .t_local label {
            width: 60px !important;
        }

        .t_local .layui-input-block {
            margin-left: 90px !important;
        }

        .layui-tab-brief {
            background: #f7f7f7;
        }

        .m_edit>div {
            background: #fff !important;
        }

        .m_c_upload {
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -60px;
            margin-top: -20px;
            width: 120px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-radius: 25px;
            background: #1E9FFF;
            color: #fff;
        }

        .screenshot {
            position: absolute;
            top: 10px;
            left: 10px;
            z-index: 9;
        }

        #m_cover_app {
            width: 100%;
            height: 600px;
            position: relative;
        }

        #m_cover_app i {
            position: absolute;
            bottom: 20px;
            right: 20px;
            cursor: pointer;
            color: #fff;
            font-weight: bold;
            font-size: 25px;
        }

        .totalOut {
            display: none;
        }

        .m_dec_title {
            font-size: 16px;
            font-weight: bold;
            margin: 10px 0px;
        }

        .navIcon {
            width: 40px;
            height: 40px;
            border: 1px #ccc solid;
            margin-left: 10px;
        }

        .funcLevel2 {
            background: #fff;
            padding: 10px;
            border-radius: 3px;
        }
    </style>
</head>

<body style="width: 100%;padding: 0px; background-color: #eaeaea;">

    <div class="m">
        <div class="m_cover">
            <!--截屏按钮-->
            <button type="button" class="layui-btn layui-btn-sm layui-btn-normal screenshot"><i
                    class="layui-icon ">&#xe65d;</i> 保存封面
            </button>
            <div id="m_cover_app">
                <iframe frameborder="none" width="100%" height="100%" id="cesiumIfm"></iframe>
                <!--全屏-->
                <i class="layui-icon totalIn">&#xe622;</i>
                <i class="layui-icon totalOut">&#xe758;</i>
            </div>
        </div>

        <div class="m_edit layui-form">
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title">
                    <li class="layui-this">基本信息</li>
                    <li>模板编辑</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div class="m_title">名称</div>
                        <input type="text" name="title" placeholder="请输入模板名称" autocomplete="off"
                            class="layui-input name">
                        <div class="m_title">描述</div>
                        <textarea type="text" name="title" placeholder="请输入模板描述" autocomplete="off"
                            class="layui-textarea desc"></textarea>
                        <div class="m_title">分类</div>
                        <select name="city" lay-verify="">
                            <!-- <option value="010">北京</option>
                            <option value="0571" selected>杭州</option> -->
                        </select>
                        <div class="m_title">模板类型</div>
                        <select name="m_type" lay-verify="" lay-filter="m_type">
                            <option value="0" selected>工程模板</option>
                            <option value="1">功能模板</option>
                        </select>
                        <div class="m_title">封面</div>
                        <div class="m_c">
                            <img>
                            <div class="m_c_upload"><i class="layui-icon">&#xe61f;</i>上传封面</div>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div class="show_data layui-form" lay-filter="mEdit">
                            <div class="m_title">初始位置</div>
                            <div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">X</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="x" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">Y</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="y" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">Z</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="z" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">yaw</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="yaw" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">pitch</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="pitch" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item local">
                                    <label class="layui-form-label">roll</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="roll" required lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <!-- <div class="m_title">工程列表</div>
                            <div class="m_engin">
                                <input type="text" name="xm" required lay-verify="required" autocomplete="off"
                                    class="layui-input">
                                <button type="button" class="layui-btn layui-btn-normal">添加项目</button>
                                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                                    <ul class="layui-tab-title">
                                        <li class="layui-this">矢量</li>
                                        <li>模型</li>
                                        <li>街景</li>
                                        <li>地形</li>
                                        <li>影像</li>
                                    </ul>
                                    <div class="layui-tab-content">
                                        <div class="layui-tab-item layui-show">
                                            <div class="m_vector">
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">数据来源</label>
                                                    <div class="layui-input-block">
                                                        <select name="sjly" lay-verify="required">
                                                            <option value=""></option>
                                                            <option value="0">公开</option>
                                                            <option value="1">私有</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">默认状态</label>
                                                    <div class="layui-input-block">
                                                        <select name="mrzt" lay-verify="required">
                                                            <option value=""></option>
                                                            <option value="0">显示</option>
                                                            <option value="1">隐藏</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">数据选择</label>
                                                    <div class="layui-input-block">
                                                        <select name="sjxz" lay-verify="required">
                                                            <option value=""></option>
                                                            <option value="0">江苏行政区划</option>
                                                            <option value="1">一级国道</option>
                                                            <option value="1">二级国道</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">样式选择</label>
                                                    <div class="layui-input-block">
                                                        <select name="ysxz" lay-verify="required">
                                                            <option value=""></option>
                                                            <option value="0">默认线</option>
                                                            <option value="1">默认点</option>
                                                            <option value="1">默认面</option>
                                                            <option value="1">模型点</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                            <button type="button" style="width: 100%"
                                                class="layui-btn layui-btn-normal">添加图层</button>
                                            <table id="t_vector" lay-filter="t_vector"></table>
                                        </div>
                                        <div class="layui-tab-item">内容2</div>
                                        <div class="layui-tab-item">内容3</div>
                                        <div class="layui-tab-item">内容4</div>
                                        <div class="layui-tab-item">内容5</div>
                                    </div>
                                </div>
                            </div> -->
                        </div>
                        <div class="show_func">
                            <div class="m_title">工具菜单</div>
                            <div class="nav_tool">
                                <select name="gjcd" lay-verify="">
                                    <option value="" selected>请选择</option>
                                    <option value="图层">图层</option>
                                    <option value="属性">属性</option>
                                    <option value="结果">结果</option>
                                    <option value="操作">操作</option>
                                </select>
                                <button type="button" class="layui-btn layui-btn-normal toolAdd">添加</button>
                                <div class="layui-tab layui-tab-brief" lay-filter="tool" lay-allowClose="true">
                                    <ul class="layui-tab-title">
                                        <li style="display: none;"></li>
                                    </ul>
                                    <div class="layui-tab-content">
                                        <div style="display: none;" class="layui-tab-item"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="m_title">功能菜单</div>
                            <div class="nav_func">
                                <select name="gncd" lay-verify="">
                                    <option value="0" selected>请选择</option>
                                    <option value="图层">测量</option>
                                    <option value="属性">分析</option>
                                    <option value="结果">图层</option>
                                    <option value="结果">标绘</option>
                                    <option value="结果">查询</option>
                                </select>
                                <button type="button" class="layui-btn layui-btn-normal funcAdd">添加</button>
                                <div class="layui-tab layui-tab-brief" lay-allowClose="true" lay-filter="func">
                                    <ul class="layui-tab-title funcul">
                                        <li style="display: none;"></li>
                                    </ul>
                                    <div class="layui-tab-content">
                                        <div style="display: none;" class="layui-tab-item"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="m_edit_btn">
                    <button type="button" class="layui-btn layui-btn-normal">重置</button>
                    <button type="button" class="layui-btn layui-btn-normal release">发布</button>
                    <button type="button" class="layui-btn layui-btn-normal save">保存</button>
                </div>
            </div>
        </div>
        <div class="m_author">
            <div class="m_name">-----</div>
            <div class="m_author_photo">
                <img src="../../static/IMG/Mould/m_author.png">
            </div>
            <div class="m_author_name">
                <p></p>
            </div>
        </div>
        <div class="m_cover"
            style=" background: #fff; box-shadow: 3px 6px 11px 0 #d4d4d4;margin-top: 20px; padding-left: 20px;box-sizing: border-box">
            <div class="m_dec_title">作品描述</div>
            <div class="m_dec_con">-----</div>
            <div class="m_dec_type">
                <div>
                    发布时间：<span class="m_dec_time">2020/01/02</span>
                </div>
                <div>
                    数据类型：<span>应用/演示应用</span>
                </div>
            </div>
        </div>
    </div>


    <script src="../../JS/jquery-2.0.3.js"></script>
    <script src="../../layui/layui.js" charset="utf-8"></script>
    <script>
        //选择的模板类型
        var m_type = '0';
        //二级菜单当前操作的tab下标
        var funcIndex = 0;
        //获取数据id并调用接口(如果存在id则是编辑页面，无id则是新增页面)
        var id = parseInt(parent.getUrlParam(window.location.search, 'id'));
        var $cesuimWin = $('#cesiumIfm')[0].contentWindow;
        var form = null;
        layui.use(['element', 'form', 'table', 'upload'], function () {
            var element = layui.element,
                table = layui.table,
                upload = layui.upload;
                form = layui.form;
            table.render({
                elem: '#t_vector'
                , height: 312
                , url: '/demo/table/user/' //数据接口
                , page: true //开启分页
                , cols: [[ //表头
                    { field: 'id', title: '序号', width: 60, fixed: 'left' }
                    , { field: 'username', title: '图层选择', width: 90 }
                    , { field: 'sex', title: '样式选择', width: 90 }
                    , { field: 'city', title: '默认状态', width: 90 }
                ]]
            });

            form.on('select(m_type)', function (data) {
                m_type = data.value;
                if (m_type === '1') {
                    $('.show_data').hide()
                    $('.show_func').show();
                    if ($cesuimWin.treeIndex) {
                        $cesuimWin.layer.close($cesuimWin.treeIndex)
                    }
                } else if (m_type === '0') {
                    $('.show_data').show()
                    $('.show_func').hide();
                    $cesuimWin.layerHandle();
                }
            })

            var active = {
                tabAdd: function (filter, id, name, fn, data) {
                    //新增一个Tab项 传入三个参数，分别对应其标题，tab页面的地址，还有一个规定的id，是标签中data-id的属性值
                    //关于tabAdd的方法所传入的参数可看layui的开发文档中基础方法部分
                    element.tabAdd(filter, {
                        title: name,
                        content: '<div data-id="' + id + '">' + fn(id, name, data) + '</div>',
                        id: id //规定好的id
                    });
                    element.render();
                    form.render();
                    if (filter === 'tool') {
                        //工具菜单删除tab
                        element.on('tabDelete(tool)', function (data) {
                            $cesuimWin.setToolNav(getToolData())
                        });
                    } else {
                        ///功能菜单二级删除tab
                        element.on('tabDelete(' + filter + ')', function (data) {
                            $cesuimWin.setFuncNav(getFuncData())
                        });
                    }
                },
                tabChange: function (filter, id) {
                    //切换到指定Tab项
                    element.tabChange(filter, id); //根据传入的id传入到指定的tab项
                }
            }

            //工具菜单新增tab
            $('.toolAdd').click(function () {
                var name = $(this).prev().find('input').val();
                var index = $(this).next().find('.layui-tab-title li').length;
                active.tabAdd('tool', index, name, toolHtml, null);
                active.tabChange('tool', index);
                //上传图片按钮初始化
                uploadImg(upload, '#toolUpload' + index);
                $cesuimWin.setToolNav(getToolData())
            })


            //功能菜单新增tab
            $('.funcAdd').click(function () {
                var name = $(this).prev().find('input').val();
                var index = $(this).next().find('.layui-tab-title li').length;
                active.tabAdd('func', index, name, funcHtml, null);
                active.tabChange('func', index);
                //上传图片按钮初始化
                uploadImg(upload, '#funcUpload' + index);
                $cesuimWin.setFuncNav(getFuncData())
            })


            //功能菜单二级新增tab
            $('.nav_func').on('click', '.funcls', function () {
                var name = $(this).prev().find('input').val();
                var index = $(this).next().find('.layui-tab-title li').length;
                //获取当前过滤器
                var filter = $(this).next().attr('lay-filter');
                funcIndex = filter.replace(/[^0-9]/ig, "");
                active.tabAdd(filter, index, name, funcls, null);
                active.tabChange(filter, index);
                //上传图片按钮初始化
                uploadImg(upload, '#funclsUpload' + funcIndex + '_' + index);
                $cesuimWin.setFuncNav(getFuncData())
            })


            //功能菜单开启二级菜单
            form.on('switch(funcSwitch)', function (data) {
                if (this.checked) {
                    data.othis.parent().parent().next().show()
                } else {
                    data.othis.parent().parent().next().hide()
                }
            });
            $("#cesiumIfm").load(function () {
                if (id) {
                    parent.ajaxGet(parent.defaultUrl + '/template/selTemplateById', { 'id': id }, function (data) {
                        if (data.code === 200 && data.data) {
                            $('.name').val(data.data.name);
                            $('.m_name').text(data.data.name);
                            $('.m_dec_con').text(data.data.desc);
                            $('.m_dec_time').text(data.data.time);
                            $('.desc').val(data.data.desc);
                            $('.m_c img').attr('src', data.data.img || '../../static/IMG/nothumb.png');
                            key = data.data.id;
                            path = data.data.path;
                            type = data.data.type;
                            menus = data.data.menus ? JSON.parse(data.data.menus) : [];
                            tools = data.data.tools ? JSON.parse(data.data.tools) : [];
                            $('select[name=m_type]').next().find('dd').eq(type).click();
                            type === '1' ? setNavdata(active, form, upload, menus, tools) : '';
                            //球上模板菜单初始化显示
                            if (tools.length > 0) {
                                $cesuimWin.setToolNav(tools)
                            }
                            if (menus.length > 0) {
                                $cesuimWin.setFuncNav(menus)
                            }
                            //工程树数据转化
                            treeData = data.data.projectTree && JSON.parse(data.data.projectTree);
                            $cesuimWin.treeInit(data.data.projectTree);
                            //球初始化定位
                            cesuim_posi = data.data.position ? JSON.parse(data.data.position) : '';
                            if (cesuim_posi) {
                                //判断获取的数据中是否有属性值为空
                                var flag = false;
                                for (key in cesuim_posi) {
                                    if (cesuim_posi[key] === '') {
                                        flag = true;
                                        break;
                                    }
                                }
                                form.val('mEdit',cesuim_posi);
                                if (!flag) {
                                    $cesuimWin.flyPosi(cesuim_posi);
                                }                          
                            }                        
                        }
                    });
                } else {
                    $('select[name=m_type]').next().find('dd').eq(0).click();
                }
            })
            $('#cesiumIfm').attr('src', '../../Cesium/index.html')
        });

        $(function () {

            //描述
            $('.m_edit').on('input propertychange', '.desc', function () {
                $('.m_dec_con').text($(this).val())
            })
            
            //名称
            $('.m_edit').on('input propertychange', '.name', function () {
                $('.m_name').text($(this).val())
            })

            //显示用户头像和昵称
            var userObj = parent.getLocalStorage('JSXM');
            $('.m_author_photo img').attr('src', userObj.userPhoto);
            $('.m_author_name p').text(userObj.userName);

            //全屏功能
            $('.totalIn').click(function () {
                $(this).hide();
                $('.totalOut').show();
                $('body').css('overflow', 'hidden');
                //获取当前屏幕的宽高
                var w = $(window).width();
                var h = $(window).height();
                //父级宽度100%
                $('.m').css({ 'width': '100%', 'margin-top': 0 });
                $('#m_cover_app').width(w).height(h).css('z-index', 1);
            })
            $('.totalOut').click(function () {
                $(this).hide();
                $('.totalIn').show();
                $('body').css('overflow-y', 'auto');
                //父级宽度100%
                $('.m').css({ 'width': '80%', 'margin-top': '20px' });
                $('#m_cover_app').width('100%').height('600px');
            })

            $('.screenshot').click(function () {
                parent.screenshots($('#cesiumIfm').contents().find('#cesiumContainer')[0], function (base64, blob) {
                    $('.m_c img').attr('src', base64);
                    imgBlob = blob
                });
            })
            //上传封面触发事件
            $('.m_c_upload').click(function () {
                parent.screenshots($('#cesiumIfm').contents().find('#cesiumContainer')[0], function (base64, blob) {
                    $('.m_c img').attr('src', base64);
                    imgBlob = blob
                });
            })

            //工具菜单顶部距离
            $('.show_func').on('input propertychange', 'input[name=dbjl]', function () {
                var val = $(this).val();
                $cesuimWin.setToolNav(getToolData())
            })

            //工具菜单左侧距离
            $('.show_func').on('input propertychange', 'input[name=zcjl]', function () {
                var val = $(this).val();
                $cesuimWin.setToolNav(getToolData())
            })

            //工具菜单右侧距离
            $('.show_func').on('input propertychange', 'input[name=ycjl]', function () {
                var val = $(this).val();
                $cesuimWin.setToolNav(getToolData())
            })

            //发布应用
            $('.release').click(function () {
                parent.sysPopupf('发布设置', parent.$('#fbLevel'), 400, 360, null, ['发布', '取消'], function (index, layero) {
                    //判断提交的模板类型
                    var nav = null; //菜单模板配置数据
                    var menus = '';
                    var tools = '';
                    var projectTree = '';
                    var position = '';
                    var type = $('select[name=m_type]').val();
                    if (type === '0') {
                        projectTree = getTree() ? JSON.stringify(getTree()) : '';
                        position = getPosition() ? JSON.stringify(getPosition()) : '';
                    } else {
                        menus = JSON.stringify(getFuncData());
                        tools = JSON.stringify(getToolData());
                    }
                    if (parent.getLocalStorage('JSXM') && parent.getLocalStorage('JSXM').id) {

                        var content = {
                            "desc": $('.desc').val(),
                            // "label": [0],
                            "name": $('.name').val(),
                            "type": type,
                            "status": 0,
                            "categories": '',
                            "menus": menus,
                            "position": position,
                            "projectTree": projectTree,
                            "tools": tools,
                        }

                        //判断有没有上传图片
                        if (!imgBlob) {
                            content.img = $('.m_c img').attr('src');
                            var obj = $.extend({ "content": JSON.stringify(content) }, {
                                'objid': id || 0,
                                "openness": parent.openness,
                                "openstaff": '',
                                'shuserid': parent.getLocalStorage('JSXM').id,
                                "shstatus": 0,
                                "type": type,
                                "img": $('.m_c img').attr('src')
                            })
                            parent.ajaxPost(parent.defaultUrl + '/template/insertCheckStatus', obj, function (data) {
                                if (data.code === 200) {
                                    parent.layer.msg('发布成功，等待审核');
                                } else if (data.message) {
                                    parent.layer.msg(data.message)
                                }
                            })
                        } else {
                            //调用上传接口
                            var formdata = new FormData();
                            formdata.append("file", imgBlob);
                            parent.ajaxFile(parent.defaultUrl + '/file/imgs', formdata, function (data) {
                                if (data.code === 200) {
                                    content.img = data.data;
                                    var obj = $.extend({ "content": JSON.stringify(content) }, {
                                        'objid': id || 0,
                                        "openness": parent.openness,
                                        "openstaff": '',
                                        'shuserid': parent.getLocalStorage('JSXM').id,
                                        "shstatus": 0,
                                        "type": type,
                                        "img": data.data
                                    })
                                    parent.ajaxPost(parent.defaultUrl + '/template/insertCheckStatus', obj, function (data) {
                                        if (data.code === 200) {
                                            parent.layer.msg('发布成功，等待审核');
                                        } else if (data.message) {
                                            parent.layer.msg(data.message)
                                        }
                                    })
                                }
                            })
                        }

                    }
                    parent.layer.close(index)
                })
            })

            //保存应用
            $('.save').click(function () {
                var projectTree = '';
                var position = '';
                //判断提交的模板类型
                var nav = null; //菜单模板配置数据
                var menus = '';
                var tools = '';
                var type = $('select[name=m_type]').val();
                if (type === '0') {
                    projectTree = getTree() ? JSON.stringify(getTree()) : '';
                        position = getPosition() ? JSON.stringify(getPosition()) : '';
                } else {
                    menus = JSON.stringify(getFuncData());
                    tools = JSON.stringify(getToolData());
                }
                //判断是新增还是更新
                if (id) {
                    dataFn(parent.defaultUrl + '/template/updateTemplate', {
                        "desc": $('.desc').val(),
                        // "label": [0],
                        "id": id,
                        "name": $('.name').val(),
                        "type": type,
                        "status": 2,
                        "categories": '',
                        "img": "string",
                        "menus": menus,
                        "position": position,
                        "projectTree": projectTree,
                        "tools": tools,
                        "userid": parent.getLocalStorage('JSXM').id
                    })

                } else {
                    dataFn(parent.defaultUrl + '/template/insertTemplate', {
                        "desc": $('.desc').val(),
                        // "label": [0],
                        "id": 0,
                        "name": $('.name').val(),
                        "type": type,
                        "status": 2,
                        "categories": '',
                        "img": "string",
                        "menus": menus,
                        "position": "",
                        "projectTree": "",
                        "tools": tools,
                        "userid": parent.getLocalStorage('JSXM').id
                    })
                }
            })

        })
        //数据的保存和发布
        var imgBlob = null;
        function dataFn(url, obj) {
            //判断是否有上传图片
            if (!imgBlob) {
                obj.img = $('.m_c img').attr('src');
                upMould(url, obj);
            } else {
                //调用上传接口
                var formdata = new FormData();
                formdata.append("file", imgBlob);
                parent.ajaxFile(parent.defaultUrl + '/file/imgs', formdata, function (data) {
                    if (data.code === 200) {
                        obj.img = data.data;
                        upMould(url, obj);
                    }
                })
            }
        }

        //调用更新数据接口
        function upMould(url, obj) {
            //调用更新数据接口
            parent.ajaxPost(url, obj, function (data) {
                if (data.code === 200) {
                    layer.msg('更新成功');
                    id = data.id ? data.id : id;
                } else if (data.message) {
                    layer.msg(data.message)
                }
            })
        }
        //获取表单数据
        function getMouldFormData() {
            //获取数据选择和数据样式类别
            //获取iframe下的数据 判断是否存在
            var data = $.extend(form.val('mEdit'));
            for (key in data) {
                if (key === '') {
                    delete (data[key])
                }
            }
            return JSON.stringify(data)
        }

        //工具菜单html
        function toolHtml(i, name, data) {
            var top = data ? data.top : '100';
            var left = data ? data.left : '20';
            var right = data ? data.right : '';
            var icon = data ? data.icon : parent.m_init_icon;
            return '         <div style="display: none;" class="layui-form-item toolItem" name="type">' +
                '                                                <input type="text" value="' + name + '">\n' +
                '         </div>  ' +
                '              <div class="layui-form-item toolItem" name="top">\n' +
                '                                            <label class="layui-form-label">顶部距离</label>\n' +
                '                                            <div class="layui-input-block">\n' +
                '                                                <input type="text" name="dbjl" value="' + top + '" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
                '                                            </div>\n' +
                '                                        </div>\n' +
                '                                        <div class="layui-form-item toolItem" name="left">\n' +
                '                                            <label class="layui-form-label">左侧距离</label>\n' +
                '                                            <div class="layui-input-block">\n' +
                '                                                <input type="text" name="zcjl" value="' + left + '" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
                '                                            </div>\n' +
                '                                        </div>\n' +
                '                                        <div class="layui-form-item toolItem" name="right">\n' +
                '                                            <label class="layui-form-label">右侧距离</label>\n' +
                '                                            <div class="layui-input-block">\n' +
                '                                                <input type="text" name="ycjl" value="' + right + '" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
                '                                            </div>\n' +
                '                                        </div>\n' +
                '                                        <div class="layui-form-item toolItem" name="icon">\n' +
                '                                            <label class="layui-form-label">图标</label>\n' +
                '                                            <div class="layui-input-block">\n' +
                '                                            <button  name="toolIcon" type="button" class="layui-btn layui-btn-normal layui-btn-sm mould_btn_upload" id="toolUpload' + i + '">\n' +
                '                                                <i class="layui-icon">&#xe67c;</i>上传图片\n' +
                '                                            </button>' +
                '<img src="' + icon + '" class="navIcon" >' +
                '                                            </div>\n' +
                '                                        </div>'
        }

        //功能菜单html
        function funcHtml(i, name, data) {
            var html = '';
            var icon = data ? data.icon : parent.m_init_icon;
            var check = data ? Boolean(data.children) : true;
            html += '       <div style="display: none;" class="layui-form-item funcItem" name="type">' +
                '            <input type="text" value="' + name + '">' +
                '            </div>' +
                '                   <div class="layui-form-item funcItem" name="icon">\n' +
                '                                            <label class="layui-form-label">图标</label>\n' +
                '                                            <button name="funcIcon" type="button" class="layui-btn layui-btn-normal layui-btn-sm mould_btn_upload" id="funcUpload' + i + '">\n' +
                '                                                <i class="layui-icon">&#xe67c;</i>上传图片\n' +
                '                                            </button>\n' +
                '<img src="' + icon + '" class="navIcon" >' +
                '                                        </div>\n' +
                '                                        <div class="layui-form-item">\n' +
                '                                             <label style="width: 115px;" class="layui-form-label">是否开启二级菜单</label>\n' +
                '                                             <div class="layui-input-block funcSwitch">\n';
            if (check) {
                html += '<input type="checkbox" checked="" lay-filter="funcSwitch" name="switch" lay-skin="switch">\n';
            } else {
                html += '<input type="checkbox"  lay-filter="funcSwitch" name="switch" lay-skin="switch">\n';
            }
            html += '                                              </div>\n' +
                '                                          </div>\n' +
                '                                        <div class="funcLevel2" style="display: ' + (check ? 'block' : 'none') + '">\n' +
                '                                            <select name="gncd" lay-verify="">\n' +
                '                                                <option value="0"  selected>请选择</option>\n' +
                '                                                <option value="图层">空间水平测量</option>\n' +
                '                                                <option value="属性">空间垂直测量</option>\n' +
                '                                                <option value="结果">体积方量测量</option>\n' +
                '                                                <option value="结果">表面积测量</option>\n' +
                '                                                <option value="结果">平面面积测量</option>\n' +
                '                                            </select>\n' +
                '                                            <button type="button" class="layui-btn layui-btn-normal funcls">添加</button>\n' +
                '                                            <div class="layui-tab" lay-allowClose="true" lay-filter="funcls' + i + '">\n' +
                '                                                <ul class="layui-tab-title">\n' +
                '                                                    <li style="display: none;"></li>\n' +
                '                                                </ul>\n' +
                '                                                <div class="layui-tab-content">\n' +
                '                                                    <div style="display: none;" class="layui-tab-item"></div>\n' +
                '                                                </div>\n' +
                '                                            </div>\n' +
                '                                        </div>'
            return html;
        }

        //功能二级菜单html
        function funcls(i, name, data) {
            var icon = data ? data.icon : parent.m_init_icon;
            return '  <div style="display: none;" class="layui-form-item funclsItem" name="type">' +
                '       <input type="text" value="' + name + '">' +
                '        </div>  ' +
                '               <div class="layui-form-item funclsItem" name="icon">\n' +
                '                                            <label class="layui-form-label">图标</label>\n' +
                '                                            <button name="funcIcon" type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="funclsUpload' + funcIndex + '_' + i + '">\n' +
                '                                                <i class="layui-icon">&#xe67c;</i>上传图片\n' +
                '                                            </button>\n' +
                '<img src="' + icon + '" class="navIcon" >'
        }

        //上传图片
        function uploadImg(upload, elem) {
            upload.render({
                elem: elem //绑定元素
                , url: parent.defaultUrl + '/file/imgs' //上传接口
                , done: function (res) {
                    if (res.code === 200) {
                        $(elem).siblings('.navIcon').attr('src', res.data);
                        layer.msg('上传成功');

                        //球上实时更新上传图标
                        var name = $(elem).attr('name');
                        switch (name) {
                            case 'toolIcon':
                                $cesuimWin.setToolNav(getToolData())
                                break;
                            case 'funcIcon':
                                $cesuimWin.setFuncNav(getFuncData())
                                break;
                        }

                    } else if (data.message) {
                        layer.msg(data.message)
                    }
                }
                , error: function () {
                    //请求异常回调
                }
            });
        }


        //菜单模板数据写入
        function setNavdata(active, form, upload, func, tool) {
            for (var i = 0, l = tool.length; i < l; i++) {
                active.tabAdd('tool', i + 1, tool[i].type, toolHtml, tool[i]);
                active.tabChange('tool', i + 1);
                //上传图片按钮初始化
                uploadImg(upload, '#toolUpload' + (i + 1))
            }
            for (var i = 0, l = func.length; i < l; i++) {
                active.tabAdd('func', i + 1, func[i].type, funcHtml, func[i]);
                if (func[i].children && func[i].children.length > 0) {
                    var child = func[i].children;
                    for (var a = 0, b = child.length; a < b; a++) {
                        active.tabAdd('funcls' + (i + 1), a + 1, child[a].type, funcls, child[a]);
                        active.tabChange('funcls' + (i + 1), a + 1);
                        //上传图片按钮初始化
                        uploadImg(upload, '#funclsUpload' + (i + 1) + '_' + (a + 1));
                    }
                }
                active.tabChange('func', i + 1);
                //上传图片按钮初始化
                uploadImg(upload, '#funcUpload' + (i + 1));
            }
        }

        //工具菜单获取当前显示tab的数据
        function getToolData() {
            var toolArr = [];
            $('.nav_tool').find('.layui-tab-item').each(function () {
                var tObj = {};
                $(this).find('.toolItem').each(function () {
                    var key = $(this).attr('name');
                    var value = '';
                    if (key === 'icon') {
                        value = $(this).find('img').attr('src');
                    } else {
                        value = $(this).find('input').val();
                    }
                    tObj[key] = value;
                })
                if ($(this).find('>div').attr('data-id')) {
                    tObj.index = $(this).find('>div').attr('data-id');
                }
                !$.isEmptyObject(tObj) ? toolArr.push(tObj) : '';
            })
            return toolArr;
        }

        //功能菜单获取数据
        function getFuncData() {
            var funcArr = [];
            $('.funcul').next().find('>div').each(function () {
                var fObj = {};
                $(this).find('.funcItem').each(function () {
                    var key = $(this).attr('name');
                    var value = '';
                    if (key === 'icon') {
                        value = $(this).find('img').attr('src');
                    } else {
                        value = $(this).find('input').val();
                    }

                    //获取二级菜单
                    //判断是否有二级菜单
                    if ($(this).parent().find('.layui-form-switch').hasClass('layui-form-onswitch')) {
                        fObj['children'] = [];
                        //遍历二级菜单
                        $(this).parent().find('.layui-tab-item').each(function () {
                            var lObj = {};
                            $(this).find('.funclsItem').each(function () {
                                var key = $(this).attr('name');
                                var value = '';
                                if (key === 'icon') {
                                    value = $(this).find('img').attr('src');
                                } else {
                                    value = $(this).find('input').val();
                                }
                                lObj[key] = value;
                                //测试阶段数据写死
                                lObj.tag = 'tag';
                                lObj.tip = 'tip';

                            })
                            !$.isEmptyObject(lObj) ? fObj['children'].push(lObj) : '';
                        })
                    } else {
                        fObj['children'] = null;
                    }
                    fObj[key] = value;
                    //测试阶段数据写死
                    fObj.tag = 'tag';
                    fObj.tip = 'tip';
                })
                !$.isEmptyObject(fObj) ? funcArr.push(fObj) : '';
            })
            return funcArr
        }

        //获取工程树的数据
        var treeData = null;
        function getTree() {
            var index = $('#cesiumIfm')[0].contentWindow.treeIndex;
            if (!index) return treeData;
            return {
                'ztree': $('#cesiumIfm')[0].contentWindow.ztreeObj.getNodes(),
                'layerObj': $('#cesiumIfm')[0].contentWindow.treeNodeObj
            };
        }

        //获取球初始定位的数据
        function getPosition() {
            return form.val('mEdit')
        }
    </script>
</body>

</html>